/*
 * Copyright 2018-9-30 20:11:44 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.utils.DictionaryUtils;
import com.lap.scp.erp.ao.ReceivablesAO;
import com.lap.scp.erp.domain.ReceivablesDO;
import com.lap.scp.erp.domain.ReceivablesDetailDO;
import com.lap.scp.erp.query.ReceivablesQueryVO;

/**
 * <pre>
 *  收款信息，记录合同的收款信息，可以有多条记录，并记录每个阶段的金额控制对象，提供 收款信息，记录合同的收款信息，可以有多条记录，并记录每个阶段的金额 新增,编辑,查询等操作。
 *  控制层统一命名规则：
 *  进入新增 	add,
 *  增加保存 	save,
 *  进入修改 	edit,
 *  修改保存 	update,
 *  查询分页 	queryPage,
 *  根据ID查询 	get,
 *  删除 		deleteById
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-9-30 20:11:44
 * @since 2.0.0
 */
@Controller
@RequestMapping("receivables")
public class ReceivablesController {
	/**
	 * 收款信息，记录合同的收款信息，可以有多条记录，并记录每个阶段的金额应用流程对象
	 */
	@Resource
	private ReceivablesAO receivablesAO;

	/**
	 * <pre>
	 * 进入新增收款信息，记录合同的收款信息，可以有多条记录，并记录每个阶段的金额
	 * </pre>
	 * 
	 * @return
	 */
	@GetMapping(value = "add")
	public ModelAndView add(Integer contractId) {
		ModelAndView andView = new ModelAndView("erp/receivablesEdit");
		ReceivablesDO receivablesDO = new ReceivablesDO();
		receivablesDO.setContractId(contractId);
		andView.addObject("receivablesDO", receivablesDO);
		return andView;
	}

	/**
	 * <pre>
	 * 新增保存收款信息，记录合同的收款信息，可以有多条记录，并记录每个阶段的金额
	 * </pre>
	 * 
	 * @param receivablesDO
	 * @param error
	 * @return
	 */
	@PostMapping(value = "add")
	@ResponseBody
	public Resp<ReceivablesDO> add(ReceivablesDO receivablesDO, BindingResult error) {
		return receivablesAO.insertReceivables(receivablesDO, error);
	}

	/**
	 * <pre>
	 * 根据ID删除收款信息，记录合同的收款信息，可以有多条记录，并记录每个阶段的金额
	 * </pre>
	 * 
	 * @param receivablesId
	 * @param map
	 * @param attributes
	 * @return
	 */
	@GetMapping(value = "delete/{receivablesId}")
	@ResponseBody
	public Resp<Integer> deleteById(@PathVariable Integer receivablesId) {
		return receivablesAO.deleteById(receivablesId);
	}

	/**
	 * <pre>
	 * 根据ID查询收款信息，记录合同的收款信息，可以有多条记录，并记录每个阶段的金额
	 * </pre>
	 * 
	 * @param receivablesId
	 * @return
	 */
	@GetMapping(value = "get/{receivablesId}")
	public ModelAndView get(@PathVariable Integer receivablesId) {
		ModelAndView andView = new ModelAndView();
		Resp<ReceivablesDO> resp = receivablesAO.queryById(receivablesId);
		if (resp.isSuccess()) {
			andView.setViewName("receivablesDetail");
			andView.addObject("receivablesDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 根据条件、分页信息查询收款信息，记录合同的收款信息，可以有多条记录，并记录每个阶段的金额
	 * </pre>
	 * 
	 * @param page
	 * @param queryVO
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView queryPage(Page page, ReceivablesQueryVO queryVO) {
		ModelAndView andView = new ModelAndView();

		PageResult<ReceivablesDO> pr = receivablesAO.queryPage(page, queryVO);
		if (pr.isSuccess()) {
			andView.setViewName("receivablesList");
			andView.addObject("queryVO", queryVO);
			andView.addObject("pr", pr);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 进入收款信息，记录合同的收款信息，可以有多条记录，并记录每个阶段的金额 编辑页面
	 * </pre>
	 * 
	 * @param receivablesId
	 * @return
	 */
	@GetMapping(value = "edit/{receivablesId}")
	public ModelAndView edit(@PathVariable Integer receivablesId) {
		ModelAndView andView = new ModelAndView();

		Resp<ReceivablesDO> resp = receivablesAO.queryById(receivablesId);
		if (resp.isSuccess()) {
			andView.setViewName("erp/receivablesEdit");
			andView.addObject("receivablesDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 编辑保存收款信息，记录合同的收款信息，可以有多条记录，并记录每个阶段的金额信息
	 * </pre>
	 * 
	 * @param receivablesId
	 * @return
	 */
	@PostMapping(value = "edit")
	@ResponseBody
	public Resp<Integer> edit(ReceivablesDO receivablesDO) {
		return receivablesAO.updateReceivables(receivablesDO);
	}

	/**
	 * <pre>
	 * 进入确认收款页面
	 * </pre>
	 * 
	 * @param receivablesId
	 * @return
	 */
	@GetMapping(value = "confirm/{receivablesId}")
	public ModelAndView confirm(@PathVariable Integer receivablesId) {
		ModelAndView andView = new ModelAndView();

		Resp<ReceivablesDO> resp = receivablesAO.queryById(receivablesId);
		if (resp.isSuccess()) {
			andView.setViewName("erp/receivablesConfirm");
			andView.addObject("receivablesDO", resp.getData());
			andView.addObject("receivablesDetailDO", new ReceivablesDetailDO());
			andView.addObject("payMap", DictionaryUtils.PAY_MAP);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

}
